package com.chuan.it_market.controller.buyer_controller;

import com.chuan.it_market.controller.BaseController;
import com.chuan.it_market.pojo.dto.ResponseDTO;
import com.chuan.it_market.pojo.entity.Address;
import com.chuan.it_market.service.AddressService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/buyer/center")
public class AddressController extends BaseController {
    @Resource
    AddressService addressService;
    @RequestMapping("addressList")
    String addressList(Model model){
        Integer uno=getUsreId();
        List<Address> addressList=addressService.selectAddressByUserno(uno);
        model.addAttribute("addressList",addressList);
        return "/buyer/center/address-list";
    }

    @RequestMapping("addressInsert")
    @ResponseBody
    ResponseDTO addressInsert(Address address){
        address.setUserno(getUsreId());
        address.setMoren("否");
        int res= addressService.addressInsert(address);
        if (res!=0){
            return ResponseDTO.ok("地址增加成功");
        }
        else
        {
            return ResponseDTO.fail("地址增加失败");
        }
    }

    @RequestMapping("addressUpdate")
    @ResponseBody
    ResponseDTO addressUpdate(Address address){
        int res=addressService.addressUpdate(address);
        if (res!=0){
            return ResponseDTO.ok("修改地址成功");
        }
        else
        {
            return ResponseDTO.fail("修改地址失败");
        }
    }

    @RequestMapping("morenAddressUpdate/{ano}")
    @ResponseBody
    ResponseDTO morenAddressUpdate(@PathVariable Integer ano){
        addressService.morenAddressUpdate(ano);
        return  ResponseDTO.ok("默认地址更改成功");
    }

    @RequestMapping("addressDelete/{ano}")
    @ResponseBody
    ResponseDTO addressDelete(@PathVariable Integer ano){
        int res =addressService.addressDelete(ano);
        if (res!=0){
            return ResponseDTO.ok("删除地址成功");
        }
        else
        {
            return ResponseDTO.fail("删除地址失败");
        }
    }

    @RequestMapping("addressByAno/{ano}")
    @ResponseBody
    ResponseDTO addressByAno(@PathVariable Integer ano){
        Address address=addressService.selectAddressByAno(ano);
       return ResponseDTO.ok("查询成功",address);
    }

}
